#include<iostream>
#include<thread>
#include<mutex>
#include<condition_variable>

using namespace std;

int main()
{
	int n = 1000;
	int x = 1;
	int flag = false;
	mutex mtx;
	condition_variable cv;

	thread t1([&]() {
			for (int i = 0; i < n; i++)
			{
				unique_lock<mutex> ul(mtx);

				if(flag)
					cv.wait(ul);

				cout << "Thread 1:" << x << endl;
				++x;
				flag = true;

				cv.notify_one();
			}
		});

	thread t2([&]() {
		for (int i = 0; i < n; i++)
		{
			unique_lock<mutex> ul(mtx);
			if(!flag)
				cv.wait(ul);

			cout << "Thread 2:" << x << endl;
			++x;
			
			flag = false;
			cv.notify_one();
		}
		});

	t1.join();
	t2.join();
	return 0;
}